package utils
{
	import com.drm.utils.DRMUtils;
	import com.drm.components.validators.DRMValidator;
	
	import flash.display.DisplayObject;
	
	import mx.controls.AdvancedDataGrid;
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.ColorPicker;
	import mx.controls.ComboBox;
	import mx.controls.DataGrid;
	import mx.controls.DateChooser;
	import mx.controls.DateField;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.LinkButton;
	import mx.controls.List;
	import mx.controls.NumericStepper;
	import mx.controls.RadioButton;
	import mx.controls.RichTextEditor;
	import mx.controls.Text;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.validators.Validator;

	public class SMContainerUtils
	{
		
		static public const VALUE_DEFAULT_LABEL:int = 10;
		
		static public function calculeWidth(value:Number, actualWidth:Number) : Number
		{
			return ((value * 100) / actualWidth) / 100;
		}
		
		static public function calculeHeight(value:Number, actualHeight:Number) : Number
		{
			return ((value * 100) / actualHeight) / 100;
		}
		
		static public function executeMaximizeInHeightForField(field:DisplayObject, value:Number, proportional:Boolean) : void
		{
			
			if (!proportional)
			{
				return;
			}
			
			var apply:Boolean = true;
			
			if (field.hasOwnProperty('ignoreMax')
				&& field['ignoreMax'])
			{
				apply = false;
			}
			
			if (apply)
			{
				
				field.y *= value;
				
				field.height *= value;
				
			}
			
		}
		
		static public function executeMaximizeInWidthForField(field:DisplayObject, value:Number) : void
		{
			
			var apply:Boolean = true;
			
			if (field.hasOwnProperty('ignoreMax')
				&& field['ignoreMax'])
			{
				apply = false;
			}
			
			if (apply)
			{
				
				field.x *= value;
				
				if (field is Label)
				{
					field.width = Label(field).text.length * VALUE_DEFAULT_LABEL;
				}
				else 
				{
					field.width *= value;
				}
				
			}
			
		}
		
		static public function resizeFieldsByContainer(displayObject:DisplayObject, tempW:Number, tempH:Number, proportional:Boolean) : void
		{
			
			var container:Container = displayObject as Container;
			
			for (var i:int = 0; i < container.numChildren; i++)
			{
				
				var field:DisplayObject = container.getChildAt(i);
				
				executeMaximizeInWidthForField(field, tempW);
				
				executeMaximizeInHeightForField(field, tempH, proportional);
				
				if (field is Container) 
				{
					resizeFieldsByContainer(Container(field), tempW, tempH, proportional);
				}
				
			}
			
		}
		
		static public function getAllTranslateFieldsByContainer(form:Container, array:Array, except:Array = null) : Array 
		{
			
			var field:DisplayObject;
			
			var children:Array = form.getChildren();
			
			for (var i:int = 0; i < children.length; i++) 
			{
				
				field = children[i];
				
				if (!SMUtils.existsInArrayExcept(field, except))
				{
					
					if (field is Container) 
					{
						getAllTranslateFieldsByContainer(Container(field), array, except);
					} 
					else if (field is Label || field is Button 
						|| field is LinkButton || field is CheckBox
						|| field is RadioButton || field is Text) 
					{
						array.push(field);
					}
					else if (field is DataGrid)
					{
						
						var columns:Array = DataGrid(field).columns;
						
						for (var j:int = 0; j < columns.length; j++) 
						{
							if (columns[j] is DataGridColumn)
							{
								array.push(DataGridColumn(columns[j]));
							}
						} 
					}
					else if (field is AdvancedDataGrid)
					{
						
						var columnsAdvanced:Array = AdvancedDataGrid(field).columns;
						
						for (var k:int = 0; k < columnsAdvanced.length; k++) 
						{
							if (columnsAdvanced[k] is AdvancedDataGridColumn)
							{
								array.push(AdvancedDataGridColumn(columnsAdvanced[k]));
							}
						} 
						
					}
					
				}
				
			}
			
			return array;
			
		}
		
		static public function getAllFieldsByContainer(form:Container, array:Array, except:Array = null) : Array 
		{
			
			var field:DisplayObject;
			
			var children:Array = form.getChildren();
			
			for (var i:int = 0; i < children.length; i++) 
			{
				
				field = children[i];
				
				if (!SMUtils.existsInArrayExcept(field, except))
				{
					
					if (field is RichTextEditor) 
					{
						array.push(field);
					}
					else if (field is Container) 
					{
						getAllFieldsByContainer(Container(field), array, except);
					} 
					else if (field is DateField || field is TextInput || field is Text || field is Label 
						|| field is Button || field is TextArea || field is ComboBox || field is CheckBox 
						|| field is RadioButton || field is DataGrid || field is List || field is NumericStepper 
						|| field is DateChooser || field is Image || field is ColorPicker) 
					{
						array.push(field);
					}
					
				}
				
			}
			
			return array;
			
		}
		
		static public function syncronizedContainerForObject(valueObject:Object, container:Container, excludeFields:Array = null) : void
		{
			
			var fields:Array = new Array();
			
			fields = getAllFieldsByContainer(container, fields, excludeFields);
			
			var field:DisplayObject;
			
			var properties:Array = DRMUtils.getPropertiesForObject(valueObject);
			
			for (var i:int = 0; i < fields.length; i++)
			{
				
				field = fields[i];
				
				if (DRMUtils.isValidPropertyForObject(field.name, properties))
				{
					
					SMComponentUtils.selectField(field, DRMUtils.findValueForObjectByFieldName(valueObject, field.name));	
					
				}
				
			}
			
		}
		
		static public function syncronizedObjectForContainer(valueObject:Object, container:Container, excludeParameters:Array = null) : Object
		{
			
			var fields:Array = new Array();
			
			fields = getAllFieldsByContainer(container, fields, excludeParameters);
			
			var field:DisplayObject;
			
			var properties:Array = DRMUtils.getPropertiesForObject(valueObject);
			
			for (var i:int = 0; i < fields.length; i++)
			{
				
				field = fields[i];
				
				if (DRMUtils.isValidPropertyForObject(field.name, properties))
				{
					
					if (field is TextInput && TextInput(field).displayAsPassword)
					{
						DRMUtils.setValuePropertyForObject(valueObject, field.name, SMComponentUtils.findValueForDisplayObject(field), false);	
					}
					else
					{
						DRMUtils.setValuePropertyForObject(valueObject, field.name, SMComponentUtils.findValueForDisplayObject(field));	
					}
					
				}
				
			}
			
			return valueObject;
			
		}
		
		static public function validationRequiredFieldsByContainer(container:Container, except:Array = null) : Boolean
		{
			
			var validators:Array = new Array();
			
			var fields:Array = getAllFieldsByContainer(container, new Array(), except);
			
			var field:Object;
			
			for (var i:int = 0; i < fields.length; i++)
			{
				
				field = fields[i];
				
				if (SMComponentUtils.isComponentRequired(field as UIComponent))
				{
					var validator:DRMValidator = new DRMValidator();
					validator.required = true;
					validator.source = field;
					validator.property = 'text';
					validator.requiredFieldError = 'Campo obrigatório.';
					validators.push(validator);
				}
				
			}
			
			var erros:Array = Validator.validateAll(validators);
			
			if (erros.length > 0)
			{
				return false;
			}
			
			return true;
			
		}
		
		static public function enableFieldsByPanel(panel:Container, except:Array = null) : void 
		{
			disableOrEnableFieldsByPanelExcept(panel, null, true);
		}
		
		static public function disableFieldsByPanel(panel:Container, except:Array = null) : void 
		{
			disableOrEnableFieldsByPanelExcept(panel, null, false);
		}
		
		static public function disableOrEnableFieldsByPanelExcept(panel:Container, except:Array = null, opcao:Boolean = false) : void 
		{
			
			var children:Array = getAllFieldsByContainer(panel, new Array(), except);
			
			var field:DisplayObject;
			
			for (var i:int = 0; i < children.length; i++) 
			{
				
				field = children[i];
				
				if (field is DateField) 
				{
					DateField(field).editable = opcao;
					
				} 
				else if (field is TextInput) 
				{
					TextInput(field).editable = opcao;
				} 
				else if (field is TextArea) 
				{
					TextArea(field).editable = opcao;
				} 
				else if (field is ComboBox) 
				{
					ComboBox(field).enabled = opcao;
				}
				else if (field is Button) 
				{
					Button(field).enabled = opcao;
				} 
				else if (field is CheckBox) 
				{
					CheckBox(field).enabled = opcao;
				} 
				else if (field is RadioButton) 
				{
					RadioButton(field).enabled = opcao;
				} 
				else if (field is DataGrid) 
				{
					DataGrid(field).allowDragSelection = opcao;
				} 
				else if (field is List) 
				{
					List(field).enabled = opcao;
				} 
				else if (field is NumericStepper) 
				{
					NumericStepper(field).enabled = opcao;
				} 
				else if (field is ColorPicker) 
				{
					ColorPicker(field).enabled = opcao;
				}
				
			}
			
		}
		
		static public function clearFieldsByPanel(form:Container, except:Array = null) : void 
		{
			
			var children:Array = getAllFieldsByContainer(form, new Array(), except);
			
			var field:DisplayObject;
			
			for (var i:int = 0; i < children.length; i++) 
			{
				
				field = children[i];
				
				if (field is DateField) 
				{
					DateField(field).selectedDate = null;
					DateField(field).text = '';
				} 
				else if (field is TextInput) 
				{
					TextInput(field).text = '';
				} 
				else if (field is NumericStepper) 
				{
					NumericStepper(field).value = 0;
				} 
				else if (field is TextArea) 
				{
					TextArea(field).text = '';
				}
				else if (field is ComboBox) 
				{
					
					var jcComboBox:ComboBox = field as ComboBox;
					
					jcComboBox.selectedIndex = 0;
					
				} 
				else if (field is List) 
				{
					List(field).selectedIndex = -1;
				} 
				else if (field is DataGrid) 
				{
					DataGrid(field).selectedIndex = -1;
				} 
				else if (field is CheckBox) 
				{
					CheckBox(field).selected = false;
				}
				else if (field is ColorPicker) 
				{
					ColorPicker(field).selectedColor = 0x000000;
					ColorPicker(field).selectedIndex = -1;
				}
				
			}
			
		}
	}
}